var ClubQuickGroupingWIndow = Window.extend({
    _ccsFileName: res.ClubQuickGroupingWIndow_json,
    _clubId: null,
    _currentSelectGroup: null,
    _notGroupMember: [],
    _selectGroupMember: null, //默认没有选中分组 所以为null  不能初始化[] ,因为有的分组没有人 也是[],

    initWindow: function (clubId) {
        this._clubId = clubId;
        this._nodeList['groupName'].setString('');
        this._nodeList['tipsLabel'].setString('当前未选择分组');

        this._notGroupMember = ClubDataManager.getClubGroupMembers(this._clubId, 0);
        this._initGroupScrollView('noGroupScrollView', this._notGroupMember, 0);
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'btn_selectGroup': '_clickSelectGroup',
            'btn_edit': '_clickEdit',
            'btn_ok': '_clickOk',
            'btn_search': '_clickSearch',
            'btn_cancel': '_clickCancel',
        }
    },

    _clickSearch: function () {
        var str = this._nodeList['searchNode.tipsLabel'].getString();
        if (str.length > 0) {
            var searchResultList = [];
            var members = ClubDataManager.getClubGroupMembers(this._clubId, 0);
            for (var i = 0; i < members.length; i++) {
                if (members[i].uid && members[i].uid.toString().indexOf(str) >= 0) {
                    searchResultList.push(members[i]);
                }
            }
            if (searchResultList.length > 0) {
                this._notGroupMember = searchResultList;
            } else {
                showAlert('没有ID为str的用户');
                this._notGroupMember = ClubDataManager.getClubGroupMembers(this._clubId, 0);
            }
        } else {
            this._notGroupMember = ClubDataManager.getClubGroupMembers(this._clubId, 0);
        }
        this._initGroupScrollView('noGroupScrollView', this._notGroupMember, 0);

    },
    _clickCancel: function () {
        this._searchUid = '';
        this._nodeList['searchNode.tipsLabel'].setString('');
        this._notGroupMember = ClubDataManager.getClubGroupMembers(this._clubId, 0);
        this._initGroupScrollView('noGroupScrollView', this._notGroupMember, 0);
    },


    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubGroupSelectEvent] = '_selecetGroupHandler';
        this._eventListeners[ClubMsgCode.AddmodifyClubGroup] = '_queryClubGroupHandler';
    },


    /**
     * 点击选择分组
     * @private
     */
    _clickSelectGroup: function () {
        showWindow('ClubGroupWindow', GroupWindowType_QuickGrouping, this._clubId);
    },

    /**
     * 点击编辑组名
     * @private
     */
    _clickEdit: function () {
        if (this._currentSelectGroup == undefined) {
            showAlert('请选一个组');
            return;
        }
        showWindow('ClubGroupChangNameWindow', this._clubId, this._currentSelectGroup);
    },

    /**
     * 确定修改
     * @private
     */
    _clickOk: function () {
        showMessageBox('是否保存修改结果?', this._saveChange.bind(this), this._cancelChange.bind(this));
    },

    /**
     * 保存修改结果
     * @private
     */
    _saveChange: function () {
        var optGroupUids = [];
        var targetGroupUids = [];

        for (var i = 0; i < this._notGroupMember.length; i++) {
            optGroupUids.push(this._notGroupMember[i].uid);
        }

        for (var i = 0; i < this._selectGroupMember.length; i++) {
            targetGroupUids.push(this._selectGroupMember[i].uid);
        }

        var set_group = {};
        set_group[0] = optGroupUids;
        set_group[this._currentSelectGroup.pos] = targetGroupUids;

        network.send(2103, {
            cmd: ClubMsgCode.AddClubGroupUids,
            club_id: this._clubId,
            set_group: set_group
        });
    },

    /**
     * 取消保存
     * @private
     */
    _cancelChange: function () {

    },


    _queryClubGroupHandler: function (data) {
        var groups = data.info['groups']['groups'] || data.info['groups'];
        var groupList = ClubDataManager.clubGroupsRefresh(this._clubId, groups);
        for (var i = 0; i < groupList.length; i++) {
            if (groupList[i].pos == this._currentSelectGroup.pos) {
                this._selecetGroupHandler(groupList[i]);
                break;
            }
        }

    },

    /**
     * 当前选中的分组
     * @param data
     * @private
     */
    _selecetGroupHandler: function (data) {
        this._currentSelectGroup = data;
        this._nodeList['groupName'].setString(this._currentSelectGroup.name);

        var members = ClubDataManager.getClubGroupMembers(this._clubId, data.pos);
        if (members.length == 0) {
            this._nodeList['tipsLabel'].setString('当前分组暂无成员');
            this._nodeList['tipsLabel'].setVisible(true);
        }
        this._selectGroupMember = members;
        this._initGroupScrollView('groupScrollView', members, data.pos);
    },

    /**
     * 初始化列表显示
     * @private
     */
    _initGroupScrollView: function (scrollViewName, members, pos) {
        var scrollView = this._nodeList[scrollViewName];
        scrollView.removeAllChildren();

        var tmpHeight = members.length * 68; // 90为ClubGroupItem对象的高度
        var innerHeight = Math.max(scrollView.getContentSize().height, tmpHeight);
        //是否是未分组列表
        var isNotGroup = scrollViewName == 'noGroupScrollView';
        for (var i = 0; i < members.length; i++) {
            var quickGroupItem = addItem('ClubQuickGroupingItem', scrollView, members[i], pos, isNotGroup, this._selectMemberHandler.bind(this));
            quickGroupItem.setPositionY(innerHeight - 68 * (i + 1));
        }
        scrollView.innerHeight = innerHeight;


        if (!isNotGroup && members.length > 0) {
            this._nodeList['tipsLabel'].setVisible(false);
        }
    },

    /**
     * 成员选择回调
     * @private
     */
    _selectMemberHandler: function (selectMemeber, pos) {

        //pos == 0 的话是从未分组 分配到当前分组
        if (pos == 0) {
            if (this._selectGroupMember == null) {
                showAlert('请先选择分配的组');
                return;
            }
            var results = this._membersHandler(this._notGroupMember, this._selectGroupMember, selectMemeber);
            this._notGroupMember = results.delectList;
            this._selectGroupMember = results.addList;
        } else {
            var results = this._membersHandler(this._selectGroupMember, this._notGroupMember, selectMemeber);
            this._selectGroupMember = results.delectList;
            this._notGroupMember = results.addList;
        }
        //刷新两边显示列表
        this._initGroupScrollView('noGroupScrollView', this._notGroupMember, 0);
        this._initGroupScrollView('groupScrollView', this._selectGroupMember, this._currentSelectGroup.pos);
    },

    /**
     * 元素列表处理
     * @param delectList  需要过滤删除的列表
     * @param addList     需要添加的列表
     * @param memeber     检测元素
     * @private
     */
    _membersHandler: function (delectList, addList, memeber) {
        var removeIndex = -1;
        for (var i = 0; i < delectList.length; i++) {
            if (delectList[i].uid == memeber.uid) {
                removeIndex = i;
                break;
            }
        }
        if (removeIndex == -1) {
            return {delectList: delectList, addList: addList};
        }
        //从未分组中删除
        delectList.splice(removeIndex, 1);
        //添加到当前选择分组中
        addList.push(memeber);
        return {delectList: delectList, addList: addList};
    }
});